import {Component, Input, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {Router} from "@angular/router";
import {Subscription} from "rxjs/Subscription";
import {HomepageService} from "@homepage/homepage.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {Subject} from "rxjs/Subject";
@Component({
  selector: 'person-fans',
  templateUrl: './homepage-fans.component.html',
  styleUrls: ['./homepage-fans.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class HomepageFansComponent implements OnInit, OnDestroy {
  public getFans: any = {};
  public getFollows: any = {};
  subscription: Subscription = new Subscription();
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  @Input() userid: string;

  constructor(public router: Router,
              public homepageService: HomepageService,
              public toolsService: ToolsService,
              public user: AuthService) {
    this.getFollows['list'] = [];
    this.getFollows['total'] = '0';
    this.getFans['list'] = [];
    this.getFans['total'] = '0';
  }

  goToFollows() {
    this.router.navigate(['/homepage/follows'], {queryParams: {userId: this.userid}});
  }

  goToFans() {
    this.router.navigate(['/homepage/fans'], {queryParams: {userId: this.userid}});
  }

  /**
   * @description  获取我的粉丝
   * @author baoww765
   * @date 2017-12-16
   * @param userId
   * */
  getFollowsFun(userId: string) {
    if (!userId) {
      return false;
    }
    this.homepageService.getFollows(userId).takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        const object = res;
        if (object.result === '0' && object.data.list.length > 0) {
          this.getFollows['list'] = object.data.list;
          this.getFollows['total'] = object.data.total;
        } else {
          this.getFollows['list'] = ['无'];
        }
      });
  }

  /**
   * @description 获取我关注的人
   * @author baoww765
   * @date 2017-12-16
   * @param userId
   * */
  getFansFun(userId: string) {
    if (!userId) {
      return false;
    }
    this.homepageService.getFans(userId).takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        const object = res;
        if (object.result === '0' && object.data.list.length > 0) {
          this.getFans['list'] = object.data.list;
          this.getFans['total'] = object.data.total;
        } else {
          this.getFans['list'] = ['无'];
        }
      });
  }

  /**
   * @description 判断进入该页面用户是自己还是他人，
   * @author baoww765
   * @date 2017-12-17
   * */
  checkAuthor() {
    return this.user.getUserId() && this.user.getUserId() === this.userid ? '我的' : '他的';

    // if(this.user.getUserId()){
    //   if(this.user.getUserId()==this.userid){
    //     return '我的'
    //   }else{
    //     return '他的'
    //   }
    // }else{
    //   return '他的'
    // }
  }

  ngOnInit() {

  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    this.toolsService.hideLoading();
  }

}







